/**
* author jwf 2016-7-8
* 用户相关的类
执行成功，则返回
{
    status:'0',
    其他字段
}
执行出错直接返回错误码

*/
angular.module('com.xbstar.user', [])
.factory('User',['$q','JFHttp','APPTools',function ($q,JFHttp,APPTools) {
    var url="http://192.168.1.101:8080";
    return{
        getUserId:function(){
            var userid=localStorage.getItem("userid");
            //test
            //userid=1;
            if(!userid)userid=null;
            return userid;
        },
        getUserInfo:function(){
            var tmp=localStorage.getItem("userinfo")|"";
            if(tmp=""){
                return "";
            }
            return JSON.parse(tmp);
        },
        login:function(){
            var deferred=$q.defer();
            var scope = "snsapi_userinfo",
            state = "_" + (+new Date());
            Wechat.auth(scope, state, function (response) {
                alert(JSON.stringify(response));
                JFHttp.post({
                    url:url+"/user/wei",
                    data:{
                        state:response.state,
                        code:response.code
                    }
                }).then(function(data){
                    var ticket=data.ticket;
                    localStorage.setItem("userid",ticket.loginkey);
                    localStorage.setItem("ticket",JSON.stringify(ticket));
                    localStorage.setItem("userinfo",json.stringify(data.data));
                   deferred.resolve(data.data);
                },function(data){
                     deferred.reject({status:data.status});
                });
            }, function (reason) {
                deferred.reject({status:403});
            });
            return deferred.promise;
        }
    }
}])